package com.vcaresolution.logistic.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.view.RedirectView;

import com.vcaresolution.logistic.form.VesselForm;
import com.vcaresolution.logistic.to.VesselTO;
import com.vcaresolution.logistic.util.ApplicationConstants;

/**
 * <code>VesselController</code> is used to track all login related actions
 *
 * @author Snehal Shah
 * @version 1.0
 * @since ShipSoft 1.0
 */
@Controller
public class VesselController extends BaseController{

	@ModelAttribute(value=ApplicationConstants.VESSELFORM_MODELATTRIBUTE)
	public VesselForm createForm(){
		return new VesselForm();
	}
	
	/**
	 * This method is called when the GET request has been made to the ADD Vessel Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_VESSEL_URL,method = RequestMethod.GET)
	public ModelAndView loadAddVessel() {
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName(ApplicationConstants.ADD_VESSEL_VIEW);
		modelAndView.addObject(ApplicationConstants.VESSELFORM_MODELATTRIBUTE, new VesselForm());
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the ADD Vessel Request
	 */
	@RequestMapping(value=ApplicationConstants.ADD_VESSEL_URL,method = RequestMethod.POST)
	public ModelAndView processAddVessel(@Valid VesselForm vesselForm, final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		System.out.println("Inside Controller :: "+result.hasErrors());
		
		if( !result.hasErrors() ){
			vesselForm.setUser(getLoggedInUser(request));
			getServiceRegistry().getVesselService().add(vesselForm);
			
			modelAndView.setView(new RedirectView(ApplicationConstants.LIST_VESSEL_URL, true, true, false));
		}
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the List Vessel Request
	 */
	@RequestMapping(value=ApplicationConstants.LIST_VESSEL_URL,method = RequestMethod.GET)
	public ModelAndView list() {
		final ModelAndView modelAndView = new ModelAndView();
		
		List<VesselTO> vesselList = getServiceRegistry().getVesselService().findByAll();
		
		modelAndView.addObject(ApplicationConstants.VESSELLIST_MODELATTRIBUTE, vesselList);
		modelAndView.setViewName(ApplicationConstants.LIST_VESSEL_VIEW);
		return modelAndView;
	}
	
	/**
	 * This method is called when the GET request has been made to the ADD Vessel Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_VESSEL_URL,method = RequestMethod.GET)
	public  ModelAndView loadEditVessel(@RequestParam(value="vesselId") String vesselID) {
		ModelAndView modelAndView = new ModelAndView();
		
		Object vesselObject = getServiceRegistry().getVesselService().findById(Integer.parseInt(vesselID));
		
		modelAndView.addObject(ApplicationConstants.VESSELFORM_MODELATTRIBUTE, vesselObject);
		modelAndView.setViewName(ApplicationConstants.EDIT_VESSEL_VIEW);
		
		return modelAndView;
	}
	
	/**
	 * This method is called when the POST request has been made to the ADD Vessel Request
	 */
	@RequestMapping(value=ApplicationConstants.EDIT_VESSEL_URL,method = RequestMethod.POST)
	public ModelAndView processEditVessel(@Valid VesselForm vesselForm, final BindingResult result,final HttpServletRequest request,final RedirectAttributes redirectAttrs) {
		ModelAndView modelAndView = new ModelAndView();
		
		System.out.println("Inside Controller :: "+result.hasErrors());
		
		if( !result.hasErrors() ){
			
			vesselForm.setUser(getLoggedInUser(request));
			
			getServiceRegistry().getVesselService().update(vesselForm);
			
			modelAndView.setView(new RedirectView(ApplicationConstants.LIST_VESSEL_URL, true, true, false));
		}
		
		return modelAndView;
	}
}